package com.framework.modules.api;

import com.framework.common.utils.ConfigConstant;
import com.framework.entity.oss.SysOssEntity;
import com.framework.exception.RRException;
import com.framework.exception.util.RespResult;
import com.framework.modules.oss.cloud.OSSFactory;
import com.framework.modules.oss.service.SysOssService;
import com.framework.service.AgentService;
import com.framework.service.TerminalInfoService;
import com.framework.service.TransactionInfoService;
import com.framework.service.yhbservice.YhbUserService;
import com.framework.utils.string.StringUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;
import java.util.Map;

/**
 * 外部暴漏接口
 */
@RestController
@RequestMapping("yhbapi")
public class ApiController {
    @Autowired
    private SysOssService sysOssService;
    @Autowired
    private AgentService agentService;

    @Autowired
    private TransactionInfoService transactionInfoService;

    @Autowired
    private TerminalInfoService terminalInfoService;

    @Autowired
    private YhbUserService yhbUserService;


    private final static String KEY = ConfigConstant.CLOUD_STORAGE_CONFIG_KEY;

    /**
     * 上传文件
     */
    @RequestMapping("/upload")
    public RespResult upload(MultipartFile file, String prefix, String token) throws Exception {
        if (file == null) {
            throw new RRException("9000", "上传文件不能为空");
        }
        if (file.isEmpty()) {
            throw new RRException("9000", "上传文件不能为空");
        }
        if (StringUtils.isBlank(prefix)) {
            throw new RRException("9000", "上传图片前缀不能为空");
        }
        //验证是否登录
        agentService.queryAgentByToken(token);
        // 上传文件
        String suffix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
        String url = OSSFactory.build().uploadSuffix(file.getBytes(), suffix, prefix);

        // 保存文件信息
        SysOssEntity ossEntity = new SysOssEntity();
        ossEntity.setUrl(url);
        ossEntity.setCreateDate(new Date());
        sysOssService.insert(ossEntity);
        return RespResult.ok().put("url", url);
    }

    /**
     * 导入乐刷大pos交易
     *
     * @param data
     * @param sign
     * @return
     */
    @RequestMapping("/addTransactionByLeShua")
    public String getTransaction(String data, String sign) {
        String str = transactionInfoService.addTransactionByLeShuaBig(data, sign);
        return str;
    }

    /**
     * 绑定台卡的终端号
     *
     * @return
     */
    @RequestMapping(value = "/bindSnAndTerminalCode", method = RequestMethod.POST)
    public RespResult bindSnAndTerminalCode(String merc_id,
                                            String merc_nm,
                                            String corg_merc_id,
                                            String bind_dt,
                                            String sn_no,
                                            String org_no,
                                            String trm_no,
                                            String SDTermNo,
                                            String TxnDate,
                                            String TxnAmt,
                                            String TxnStatus,
                                            String TxnLogId) {

        if(!StringUtil.isEmpty(merc_id)){
            Map<String, Object> map = terminalInfoService.bindSnAndTerminalCode(merc_id,
                    merc_nm,
                    corg_merc_id,
                    bind_dt,
                    sn_no,
                    org_no,
                    trm_no);
        }

        Map<String, Object> map =transactionInfoService.getStarPosTransaction(SDTermNo,
                TxnDate,
                TxnAmt,
                TxnStatus,
                TxnLogId);

        return RespResult.ok(map);
    }

    /**
     * 上传文件
     */
    @RequestMapping("/yhbUpload")
    public RespResult yhbUpload(MultipartFile file, String prefix, String token) throws Exception {
        if (file == null) {
            throw new RRException("9000", "上传文件不能为空");
        }
        if (file.isEmpty()) {
            throw new RRException("9000", "上传文件不能为空");
        }
        if (StringUtils.isBlank(prefix)) {
            throw new RRException("9000", "上传图片前缀不能为空");
        }
        //验证是否登录
        yhbUserService.queryUserByToken(token);
        // 上传文件
        String suffix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
        String url = OSSFactory.build().uploadSuffix(file.getBytes(), suffix, prefix);

        // 保存文件信息
        SysOssEntity ossEntity = new SysOssEntity();
        ossEntity.setUrl(url);
        ossEntity.setCreateDate(new Date());
        sysOssService.insert(ossEntity);
        return RespResult.ok().put("url", url);
    }


}
